local xiaoji = fk.CreateSkill {
  name = "lvshi__xiaoji",
}

Fk:loadTranslationTable {
  ["lvshi__xiaoji"] = "枭姬",
  [":lvshi__xiaoji"] = "出牌阶段，你可将一张装备区没有的花色的非装备牌置入你的空装备栏；"..
  "当你每回合首次使用一种类别的牌后，你摸X张牌，然后你须使用一张【杀】，否则你弃置等量的牌。（X为你装备区内此花色的牌数+1）",

  ["weapon__xiaoji"] = "武器",
  ["armor__xiaoji"] = "防具",
  ["offensive_horse__xiaoji"] = "进攻坐骑",
  ["defensive_horse__xiaoji"] = "防御坐骑",
  ["treasure__xiaoji"] = "宝物",

  [":weapon__xiaoji"] = "武器（衍生牌）",
  [":armor__xiaoji"] = "防具（衍生牌）",
  [":offensive_horse__xiaoji"] = "进攻坐骑（衍生牌）",
  [":defensive_horse__xiaoji"] = "防御坐骑（衍生牌）",
  [":treasure__xiaoji"] = "宝物（衍生牌）",

  ["#lvshi__xiaoji"] = "枭姬：将一张手牌置入你的装备区",
  ["#lvshi__xiaoji-use"] = "你须使用一张【杀】，否则你弃置%arg张牌",

  ["$lvshi__xiaoji1"] = "大小姐驾到，通通闪开！",
  ["$lvshi__xiaoji2"] = "送你个轰轰烈烈的退场，感谢本小姐大恩大德吧！",
}

xiaoji:addEffect("active", {
  anim_type = "support",
  prompt = "#lvshi__xiaoji",
  max_phase_use_time = 1,
  card_num = 1,
  target_num = 0,
  can_use = function (self, player)
    return player:hasEmptyEquipSlot()
  end,
  interaction = function(self, player)
    local emptyslots = {}
    for _, slot in ipairs(player:getAvailableEquipSlots()) do
      if player:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(slot)) then
        table.insert(emptyslots, slot)
      end
    end
    return UI.ComboBox { choices = emptyslots }
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type ~= Card.TypeEquip
    and table.every(player:getCardIds("e"), function (id)
      return Fk:getCardById(id).suit ~= Fk:getCardById(to_select).suit
    end)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local card = Fk:getCardById(effect.cards[1])
    local mapper = {
      [Player.WeaponSlot] = "weapon",
      [Player.ArmorSlot] = "armor",
      [Player.OffensiveRideSlot] = "offensive_horse",
      [Player.DefensiveRideSlot] = "defensive_horse",
      [Player.TreasureSlot] = "treasure",
    }
    local card = Fk:cloneCard(mapper[self.interaction.data].."__xiaoji")
    card:addSubcards(effect.cards)
    room:moveCardIntoEquip(player, card, xiaoji.name, true, player, card)
  end,
})

xiaoji:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(xiaoji.name) and #player:getCardIds("e") > 0 then
      local use_events = player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data
        return use.from == player and use.card.type == data.card.type
      end, Player.HistoryTurn)
      return #use_events == 1 and use_events[1].data == data
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = #table.filter(player:getCardIds("e"), function (id) return Fk:getCardById(id).suit == data.card.suit end) + 1
    --if n == 0 then return end
    player:drawCards(n, xiaoji.name)
    local use = room:askToPlayCard(player, {
      pattern = "slash",
      skill_name = xiaoji.name,
      prompt = "#lvshi__xiaoji-use:::"..n,
      extra_data = { extraUse = true, bypass_times = true },
    })
    if not use then
      room:askToDiscard(player, {
        skill_name = xiaoji.name,
        cancelable = false,
        min_num = n,
        max_num = n,
        include_equip = true,
      })
    end
  end,
})

return xiaoji
